/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Menu_ImSSL.java
 *
 * Created on 17 mars 2009, 09:04:59
 */

package client;

import java.awt.*;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import javax.swing.*;

/**
 *
 * @author mailleta
 */
public class Menu_ImSSL_GUI extends javax.swing.JFrame {

    /** Creates new form Menu_ImSSL */
    private Client client;
    private Authentification_GUI auth;
    private HashMap<String,InitTabCanal> canalHash ;
    private HashMap<String,InitTabDialoguer> canalDialoguer ;
    RejoindreCreerCanal_GUI rj ;
    

    public Menu_ImSSL_GUI(Client cl) {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
            javax.swing.JOptionPane.showMessageDialog(null, "Impossible de charger le theme");
        }
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        setVisible(true);
        this.client = cl;
        client.addGui(this);
        canalHash = new HashMap<String, InitTabCanal>();
        canalDialoguer = new HashMap<String, InitTabDialoguer>();

        auth = new Authentification_GUI(this, false , client);
    }

    void bannirOk() {
        javax.swing.JOptionPane.showMessageDialog(null, "l'utilisateur a bien été bannis");
    }

    void connectionOk() {
        canal_menu.setEnabled(true);
        seConnecter.setEnabled(false);
        javax.swing.JOptionPane.showMessageDialog(null, "Connection réussie !");
        auth.dispose();
        rj = new RejoindreCreerCanal_GUI(this, false, client);

    }

    void debannirOK() {
        javax.swing.JOptionPane.showMessageDialog(null, "L'utilsateur a été debannis");
    }


    void detruireCanal(String nomCanal) {
        InitTabCanal it =  canalHash.get(nomCanal);
        canalHash.remove(nomCanal);
        chat_tabbedPane.remove(it);

    }

    void dialoguer(String pseudo) {
        InitTabDialoguer itd = new InitTabDialoguer(pseudo,client,this);
        canalDialoguer.put(pseudo,itd);
        chat_tabbedPane.addTab(pseudo, itd);
    }

    void erreurBanni() {
        rj.dispose();
        javax.swing.JOptionPane.showMessageDialog(null, "Vous avez été bannis");
    }

    void erreurConnection() {
        javax.swing.JOptionPane.showMessageDialog(null, "Le login n'existe pas ou le mot de passe est incorrect");
    }

    void erreurDroitsInsufisant() {
        javax.swing.JOptionPane.showMessageDialog(null, "vous n'avez pas les droits neccesaires pour faire cette action");
    }

    void erreurExpulser(String nomCanal) {
        javax.swing.JOptionPane.showMessageDialog(null, "vous avez été expulsé");
        InitTabCanal it = canalHash.get(nomCanal);
        canalHash.remove(nomCanal);
        chat_tabbedPane.remove(it);
    }

    void erreurInscription() {
        javax.swing.JOptionPane.showMessageDialog(null, "Le login existe déjà");
    }

    void inscriptionOk() {
        canal_menu.setEnabled(true);
        seConnecter.setEnabled(false);
        javax.swing.JOptionPane.showMessageDialog(null, "Inscription réussie !");
        rj = new RejoindreCreerCanal_GUI(this, false, client);
    }

    void rafraichirCanal(String nomCanal, Vector<String> vect) {
        rj.dispose();
        InitTabCanal it = canalHash.get(nomCanal);
        if ( it == null )
        {
            it = new InitTabCanal(nomCanal,vect,client,this);
            canalHash.put(nomCanal, it);
            chat_tabbedPane.addTab('#'+nomCanal, it);
        }
        else {
            it.changerModel(vect);
        }
    }

    void rafraichirMessageCanal(String pseudo , String nomCanal, String msg) {
        InitTabCanal it = canalHash.get(nomCanal);
        if ( it != null )
        {
            it.nouveauMessage(pseudo , msg);
        }
    }

    void rafraichirMessageutilisateur(String pseudo, String pseudoReceveur, String message) {
        InitTabDialoguer itd = canalDialoguer.get(pseudo);
        InitTabDialoguer itdReceveur = canalDialoguer.get(pseudoReceveur);
        if ( itd != null )
        {
            itd.nouveauMessage(pseudo,message);
        }
        else if (itdReceveur != null)
        {
            itdReceveur.nouveauMessage(pseudo, message);
        }
    }

    void supprimerCanal(String nomCanal) {
        javax.swing.JOptionPane.showMessageDialog(null, "le Canal a été supprimé ");
        InitTabCanal it = canalHash.get(nomCanal);
        canalHash.remove(nomCanal);
        chat_tabbedPane.remove(it);
    }

    void supprimerDialogue(String nomCanal) {
        InitTabDialoguer it = canalDialoguer.get(nomCanal);
        chat_tabbedPane.remove(it);
    }

   

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chat_tabbedPane = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        imssl_menu = new javax.swing.JMenu();
        seConnecter = new javax.swing.JMenuItem();
        apropos_item = new javax.swing.JMenuItem();
        quitter_item = new javax.swing.JMenuItem();
        canal_menu = new javax.swing.JMenu();
        creerRejoindre_item = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("ImSSL Chat");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        imssl_menu.setText("ImSSL");

        seConnecter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/connect.png"))); // NOI18N
        seConnecter.setText("Se connecter");
        seConnecter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seConnecterActionPerformed(evt);
            }
        });
        imssl_menu.add(seConnecter);

        apropos_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/apropos.png"))); // NOI18N
        apropos_item.setText("A propos");
        apropos_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apropos_itemActionPerformed(evt);
            }
        });
        imssl_menu.add(apropos_item);

        quitter_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/quitter.png"))); // NOI18N
        quitter_item.setText("Quitter");
        quitter_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitter_itemActionPerformed(evt);
            }
        });
        imssl_menu.add(quitter_item);

        jMenuBar1.add(imssl_menu);

        canal_menu.setText("Canal");
        canal_menu.setEnabled(false);

        creerRejoindre_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/plus_icon.gif"))); // NOI18N
        creerRejoindre_item.setText("Créer / Rejoindre");
        creerRejoindre_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                creerRejoindre_itemActionPerformed(evt);
            }
        });
        canal_menu.add(creerRejoindre_item);

        jMenuBar1.add(canal_menu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chat_tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 1251, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(chat_tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 717, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void quitter_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitter_itemActionPerformed
        // TODO add your handling code here:
        if(client.connecte()) {
            client.deconnecterChat();
        } else {
            client.closeSocket();
        }
    }//GEN-LAST:event_quitter_itemActionPerformed

    private void creerRejoindre_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_creerRejoindre_itemActionPerformed
        rj = new RejoindreCreerCanal_GUI(this, false, client);
    }//GEN-LAST:event_creerRejoindre_itemActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        
    }//GEN-LAST:event_formWindowClosed

    private void seConnecterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seConnecterActionPerformed
        auth = new Authentification_GUI(this, false , client);
}//GEN-LAST:event_seConnecterActionPerformed

    private void apropos_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apropos_itemActionPerformed
        // TODO add your handling code here:
        APropos ctr = new APropos(this, true);
        ctr.setVisible(true);
        ctr = null;
}//GEN-LAST:event_apropos_itemActionPerformed

    /*    */

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if(client.connecte()) {
            client.deconnecterChat();
        } else {
            client.closeSocket();
        }
    }//GEN-LAST:event_formWindowClosing

    


    /**
    * @param args the command line arguments
    */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Menu_ImSSL_GUI().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem apropos_item;
    private javax.swing.JMenu canal_menu;
    private javax.swing.JTabbedPane chat_tabbedPane;
    private javax.swing.JMenuItem creerRejoindre_item;
    private javax.swing.JMenu imssl_menu;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem quitter_item;
    private javax.swing.JMenuItem seConnecter;
    // End of variables declaration//GEN-END:variables

}
